package cn.ztq.test0614.ui.home;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.ztq.test0614.R;

public class RVDeviceAdapter extends RecyclerView.Adapter<RVDeviceAdapter.RVDeviceViewHolder> {
    private List<BluetoothDevice> list;

    public RVDeviceAdapter() {
        this.list = new ArrayList<>();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<BluetoothDevice> lists) {
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
        list.addAll(lists);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RVDeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_list, parent, false);
        return new RVDeviceViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RVDeviceViewHolder holder, int position) {
        BluetoothDevice bluetoothDevice = list.get(position);
        holder.name.setText(bluetoothDevice.getName());
        holder.address.setText(bluetoothDevice.getAddress() );

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class RVDeviceViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public Button connect;
        public TextView address;

        public RVDeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            connect = itemView.findViewById(R.id.connect);
            address = itemView.findViewById(R.id.address);
            connect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("-----", String.valueOf(getAdapterPosition()));
                }
            });
            ;
        }
    }
}
